package a51qindian.com.comprehensive.activity;

import android.content.Intent;
import android.widget.EditText;

import com.li.widget.MultipleLayout;
import com.li.widget.TopNavigationBar;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.base.QDBaseActivity;
import a51qindian.com.comprehensive.net.ObservableHelper;
import a51qindian.com.comprehensive.net.RetrofitCreator;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

/**
 * Author: Li
 * Date: 2017/10/20 0020
 * Description: 完成任务界面
 */

public class FinishRepairTaskActivity extends QDBaseActivity {

    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.et_remark)
    EditText mEtRemark;
    @BindView(R.id.layout_content)
    MultipleLayout mLayoutContent;
    private String mId;
    private String mRpId;
    private int mPos;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_finish_repair_task;
    }
    @Override
    protected void initSth() {
        super.initSth();
        mId = getIntent().getStringExtra("uid");
        mRpId = getIntent().getStringExtra("rp_id");
        mPos = getIntent().getIntExtra("pos", -1);
        mTopNavigation.setNavigationBarListener(this);
    }

    /**
     * 处理维修任务
     *
     * @param uid    用户id
     * @param rpId   维修记录id
     * @param status 状态1：已接受 2：已完成
     */
    private void handleTask(String uid, String rpId, String status,String reason) {

        Map<String, String> map = new HashMap<>();
        map.put("uid", uid);
        map.put("rp_id", rpId);
        map.put("status", status);
        map.put("reason",reason);
        mLayoutContent.onLoadingWithContent();
        ObservableHelper.createObservable(RetrofitCreator.createService().handleRepairTask(map))
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(@NonNull String s) {
                        mLayoutContent.onSuccess();
                        try {
                            JSONObject object = new JSONObject(s);
                            int code = object.getInt("code");
                            showToast(object.getString("info"));
                            if (code != 1) {

                                return;
                            }
                            Intent result=new Intent();
                            result.putExtra("pos",mPos);
                            setResult(RESULT_OK,result);
                            finish();

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mLayoutContent.onSuccess();
                        showToast(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }



    @OnClick(R.id.btn_confirm)
    public void onViewClicked() {
        handleTask(mId,mRpId,"2",mEtRemark.getText().toString());
    }
}
